package com.share.hope.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.share.hope.entity.Vocabulary;
import com.share.hope.entity.page.Condition;
import com.share.hope.entity.page.PageQueryVo;
import com.share.hope.entity.result.PageR;
import com.share.hope.entity.result.R;
import com.share.hope.mapper.VocabularyMapper;
import com.share.hope.service.VocabularyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class VocabularyServiceImpl extends BaseServiceImpl<VocabularyMapper, Vocabulary> implements VocabularyService {

    @Override
    public R<String> createVocabulary(Vocabulary vocabulary) {
        if (this.save(vocabulary)) {
            return R.ok("添加成功");
        }
        return R.error("添加失敗");
    }


    @Override
    public R<String> updateVocabulary(Vocabulary vocabulary) {
        if (this.updateById(vocabulary)) {
            return R.ok("修改成功");
        }
        return R.error("修改失敗");
    }

    @Override
    public R<String> deleteVocabulary(Long id) {
        if (this.removeById(id)) {
            return R.ok("刪除成功");
        }
        return R.error("刪除失敗");
    }

    @Override
    public R<PageR<Vocabulary>> pageVocabulary(PageQueryVo<Vocabulary> queryVo) {
        Vocabulary query = queryVo.getQuery();
        LambdaQueryWrapper<Vocabulary> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(query.getWord()), Vocabulary::getWord, query.getWord());
        wrapper.eq(!ObjectUtils.isEmpty(query.getId()), Vocabulary::getId, query.getId());
        wrapper.orderByDesc(Vocabulary::getCreateTime);
        return R.success(new PageR<>(this.page(Condition.getPage(queryVo.getPage()), wrapper)));
    }
}
